import defaultSettings from '@/settings';
import useDynamicTitle from '@/utils/useDynamicTitle';

import {defineStore} from 'pinia';

const {sideTheme, showSettings, topNav, tagsView, fixedHeader, sidebarLogo, dynamicTitle,user,userKey,userToken} = defaultSettings;

const storageSetting = JSON.parse(localStorage.getItem('layout-setting') as any) || '';

const useSettingsStore = defineStore('settings', {
	state: () => ({
		title: '',
		theme: '#4ABEFF',
		sideTheme: storageSetting.sideTheme || sideTheme,
		showSettings,
		topNav: storageSetting.topNav || topNav,
		tagsView: storageSetting.tagsView || tagsView,
		fixedHeader: storageSetting.fixedHeader || fixedHeader,
		sidebarLogo: storageSetting.sidebarLogo || sidebarLogo,
		dynamicTitle: storageSetting.dynamicTitle || dynamicTitle,
		user: storageSetting.user || user,
		userKey: storageSetting.userKey || userKey,
		userToken: storageSetting.userToken || userToken,
	}),
	actions: {
		// 修改布局设置
		changeSetting(data: {key: any; value: any}) {
			const {key, value} = data;
			if (Object.prototype.hasOwnProperty.call(this, key)) {
				this[key] = value;
			}
		},
		// 设置网页标题
		setTitle(title: any) {
			this.title = title;
			useDynamicTitle();
		},
	},
});

export default useSettingsStore;
